package com.lujianfei.netease_android_canvas.utils

import java.lang.Math.sqrt

object BezierUtils {


    fun quadBezier(t: Float, p0: Float, p1: Float, p2: Float): Float {
        return (1f - t) * (1f - t) * p0 + 2 * t * (1f - t) * p1 + t * t * p2
    }

    /**
     * 根据输入反算 t
     * 参考: https://segmentfault.com/a/1190000020612377?utm_source=tag-newest
     */
    fun getT(input: Float, p0: Float, p1: Float, p2: Float): Float {
        return getTproccess(a = p2 + p0 - 2 * p1, b = 2 * (p1 - p0), c = p0 - input)
    }

    private fun getTproccess(a: Float, b: Float, c: Float): Float {
        return (-b + kotlin.math.sqrt(b * b - 4f * a * c)) / (2 * a)
    }
}